package com.example.common.common.widget.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.common.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Hongqian.wang on 2017/9/2.
 */

public abstract class RecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder<T>> implements
        View.OnClickListener, View.OnLongClickListener, AdapterCallBack<T> {
    private List<T> dataList = new ArrayList();
    private AdapterListener<T> listener;


    public RecyclerAdapter() {
        this(null);
    }

    public RecyclerAdapter(AdapterListener<T> listener) {
        this(new ArrayList<T>(), listener);
    }

    public RecyclerAdapter(List<T> dataList, AdapterListener<T> listener) {
        this.dataList = dataList;
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return getItemViewType(position, dataList.get(position));
    }

    protected abstract int getItemViewType(int position, T t);

    /**
     * 约定ViewType的值为layout布局的int值  把布局id为ViewType的文件初始化为一个root View
     *
     * @param parent
     * @param viewType
     * @return
     */

    @Override
    public ViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        ViewHolder<T> holder = onCreateViewHolder(root, viewType);

        root.setOnClickListener(this);
        root.setOnLongClickListener(this);
        //设置View的Tag为ViewHolder 进行双向绑定
        root.setTag(R.id.tag_recycler_holder, holder);

        holder.unBinder = ButterKnife.bind(holder, root);
        holder.callBack = this;
        return holder;
    }

    public abstract ViewHolder<T> onCreateViewHolder(View root, int ViewType);

    /**
     * 绑定一个holder
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(ViewHolder<T> holder, int position) {
        T t = dataList.get(position);  //得到要绑定的数据
        holder.bind(t);   //触发holder的绑定方法

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public  List<T> getItems(){
        return dataList;
    }

    @Override
    public void update(T t, ViewHolder<T> holder) {
        int pos = holder.getAdapterPosition();
        if (pos >= 0) {
            //进行数据的移除与更新
            dataList.remove(pos);
            dataList.add(pos, t);
            notifyItemChanged(pos);
        }
    }

    @Override
    public void onClick(View view) {
        if (listener != null) {
            ViewHolder holder = (ViewHolder) view.getTag(R.id.tag_recycler_holder);
            int adapterPosition = holder.getAdapterPosition();
            listener.onItemClick(holder, dataList.get(adapterPosition));
        }
    }

    @Override
    public boolean onLongClick(View view) {
        if (listener != null) {
            ViewHolder holder = (ViewHolder) view.getTag(R.id.tag_recycler_holder);
            int adapterPosition = holder.getAdapterPosition();
            listener.onItemLongClick(holder, dataList.get(adapterPosition));
        }

        return false;
    }

    public interface AdapterListener<T> {
        void onItemClick(RecyclerAdapter.ViewHolder holder, T t);

        Void onItemLongClick(RecyclerAdapter.ViewHolder holder, T t);
    }

    public void setAdapterListener(AdapterListener<T> listener) {
        this.listener = listener;
    }

    public static abstract class ViewHolder<T> extends RecyclerView.ViewHolder {
        private Unbinder unBinder;
        protected T t;
        private AdapterCallBack<T> callBack;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        /**
         * 用于绑定数据的触发
         *
         * @param t 绑定的数据
         */
        void bind(T t) {
            this.t = t;
            onBind(t);
        }

        /**
         * 当触发绑定数据的时候的回调
         *
         * @param t
         */
        public abstract void onBind(T t);

        public void updateData(T t) {
            if (this.callBack != null) {
                callBack.update(t, this);
            }
        }
    }


    public void add(T t) {
        dataList.add(t);
        notifyItemInserted(this.dataList.size() - 1);
    }

    public void add(T... dateList) {
        if (dateList != null && dateList.length > 0) {
            int startPos = this.dataList.size();
            Collections.addAll(this.dataList, dateList);
            notifyItemRangeChanged(startPos, dateList.length);
            notifyDataSetChanged();
        }

    }

    public void add(Collection<T> dateList) {
        if (dateList != null && dateList.size() > 0) {
            int startPos = this.dataList.size();
            Collections.addAll(dateList);
            notifyItemRangeChanged(startPos, dateList.size());
            notifyDataSetChanged();
        }

    }

    /**
     * 删除操作
     */
    public void clear() {
        this.dataList.clear();
        notifyDataSetChanged();
    }

    /**
     * 替换为一个新的集合
     */

   public void replace(Collection<T> dataList) {
       this.dataList.clear();
        if (dataList == null || dataList.size() == 0) {
            return;
        }
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    /**
     * 对回掉接口做一次实现   abstract让其至少复写一个方法(不太准确)
     * @param <Data>
     */

    public  static abstract class AdapterListenerImpl<Data> implements  AdapterListener<Data>{

        @Override
        public void onItemClick(ViewHolder holder, Data data) {

        }

        @Override
        public Void onItemLongClick(ViewHolder holder, Data data) {
            return null;
        }
    }
}
